package com.test.daily.leetcode.y2021.m12.day1214;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2021/12/14 下午 01:13
 * @version: 1.0
 */
public class Code01_ScheduleCourse {
    public static void main(String[] args) {
        int[][] courses = new int[][]{{100, 200}, {200, 1300}, {1000, 1250}, {2000, 3200}};



    }
    public int scheduleCourse(int[][] courses) {

        Arrays.sort(courses, (a, b) -> a[1] - b[1]);

        PriorityQueue<Integer> q = new PriorityQueue<Integer>((a, b) -> b - a);
        // 优先队列中所有课程的总时间
        int total = 0;

        for (int[] course : courses) {
            int ti = course[0], di = course[1];
            if (total + ti <= di) {
                total += ti;
                q.offer(ti);
            } else if (!q.isEmpty() && q.peek() > ti) {
                total -= q.poll() - ti;
                q.offer(ti);
            }
        }
        return q.size();
    }
}
